﻿using Blazored.LocalStorage;
using BootstrapBlazor.Components;
using BPM.Web.Shared.Auth;
using BPM.Web.Shared.Data;
using BPM.Web.Shared.Service;
using Microsoft.AspNetCore.Components.Authorization;
using Microsoft.Extensions.DependencyInjection;

namespace BPM.Web.Shared.Extensions;

public static class ServiceExtensions
{
    public static void AddBpmBlazor(this IServiceCollection services, IDictionary<string, object> paras)
    {
        var blazorMode = paras.GetString("BlazorMode") ?? "server";
        var serverUrl = paras.GetString("ServerUrl") ?? "http://localhost:5000";

        //bootstrap组件
        //services.AddLogging(builder => builder.AddFileLogger());
        services.AddOptions();
        services.Configure<AppOptions>(options => { });
        services.AddBootstrapBlazor(op =>
        {
            op.DefaultCultureInfo = "zh";
            op.ToastPlacement = Placement.TopEnd;
            op.ToastDelay = 5000;
        }).AddBootstrapBlazorExtend();
        services.AddSingleton<ToastService>();
        services.AddBlazoredLocalStorage();

        services.AddAuthorizationCore();
        services.AddScoped<JwtAuthenticationStateProvider>();
        services.AddScoped<AuthenticationStateProvider, JwtAuthenticationStateProvider>();
        services.AddScoped<ITokenInterceptor, TokenInterceptor>();
        services.AddDependencyInjection(x =>
        {
            x.GlobalFilters.Add(new LogFilter());
            x.HttpHost = new Uri(serverUrl);
            x.Properties.Add("Cache-Control", "no-cache");
            x.Properties.Add("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)");
        });
    }
}

